package com.example.mobile1931605070zh0603;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvMainTitle;
    private Button btnMainPlayer;
    private Button btnMainPause;
    private Button btnMainRePlayer;
    private Button btnMainStop;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initMusic();
        btnMainPause.setOnClickListener(this);
        btnMainPlayer.setOnClickListener(this);
        btnMainRePlayer.setOnClickListener(this);
        btnMainStop.setOnClickListener(this);
    }

    private void initMusic() {
        mediaPlayer = MediaPlayer.create(this, R.raw.old_boy);

    }

    @Override
    public void onClick(View v) {
     switch (v.getId()){
         case R.id.btnMainPlayer:
             if (mediaPlayer==null){
                 mediaPlayer = MediaPlayer.create(this, R.raw.old_boy);
             }
             mediaPlayer.start();
             break;
         case R.id.btnMainPause:
             if (mediaPlayer!=null&&mediaPlayer.isPlaying()){
                 mediaPlayer.pause();
             }
             break;
         case R.id.btnMainRePlayer:
             if (mediaPlayer!=null){
                 mediaPlayer.seekTo(0);
                 mediaPlayer.start();
             }
             break;
         case R.id.btnMainStop:
             if (mediaPlayer!=null){
                 mediaPlayer.stop();
                 mediaPlayer.release();
                 mediaPlayer = null;
             }
             break;
     }
    }
    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        btnMainPlayer = (Button) findViewById(R.id.btnMainPlayer);
        btnMainPause = (Button) findViewById(R.id.btnMainPause);
        btnMainRePlayer = (Button) findViewById(R.id.btnMainRePlayer);
        btnMainStop = (Button) findViewById(R.id.btnMainStop);
    }
}
